package acme.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import acme.entity.Brand;

import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;

public class BrandDAOImpl extends SimpleDB implements CrudDao<Brand>{
	private static final String DOMAIN = "Brand";
	
	private static final String Brand_ID = "BrandId";
	private static final String NAME = "Name";
	private static final String DESCRIPTION = "Description";
		
	public BrandDAOImpl() {
		super();
		initDomain(DOMAIN);
	}
	
	@Override
	public Class<Brand> getClazz() {
		return Brand.class;
	}

	@Override
	public Brand createResource(Brand brand) {
		PutAttributesRequest putAttributesRequest = new PutAttributesRequest();
		putAttributesRequest.setDomainName(DOMAIN);
		putAttributesRequest.setItemName(brand.getId());
		putAttributesRequest.setAttributes(this.store(brand));
		sdb.putAttributes(putAttributesRequest);
		return brand;
	}

	@Override
	public Collection<Brand> getAllResources() {
		Collection<Brand> collection = new ArrayList<Brand>();
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN;
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		for(Item item : list){
			Brand brand = this.load(item.getAttributes());
			collection.add(brand);
		}
		return collection;
	}

	@Override
	public Brand readResource(String departmentId) {
		Brand brand = null;
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN + " where " + Brand_ID + " = '" + departmentId + "'";
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		if(list.size() > 0){
			Item item = list.get(0);
			brand = this.load(item.getAttributes());
		}
		return brand;
	}

	@Override
	public void updateResource(Brand brand) {
		this.createResource(brand);
	}

	@Override
	public void deleteResource(String ticker) {
		DeleteAttributesRequest deleteAttributesRequest = new DeleteAttributesRequest(DOMAIN, ticker);
		sdb.deleteAttributes(deleteAttributesRequest);
	}

	private Collection<ReplaceableAttribute> store(Brand brand){
		List<ReplaceableAttribute> list = new ArrayList<ReplaceableAttribute>();
		ReplaceableAttribute ra = new ReplaceableAttribute(Brand_ID, brand.getId(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(NAME, brand.getName(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(DESCRIPTION, brand.getDescription(), true);
		list.add(ra);
		return list;
	}
	
	private Brand load(List<Attribute> list){
		Brand Brand = new Brand();
		for(Attribute ra : list){
			if(ra.getName().equals(Brand_ID)){
				Brand.setBrandId(ra.getValue());
				continue;
			}
			if(ra.getName().equals(NAME)){
				Brand.setName(ra.getValue());
				continue;
			}
			if(ra.getName().equals(DESCRIPTION)){
				Brand.setDescription(ra.getValue());
				continue;
			}
		}
		return Brand;
	}
	
}
